package com.blog.admin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blog.admin.mapper.PermissionMapper;
import com.blog.admin.mapper.RoleMapper;
import com.blog.admin.mapper.RolePermissionMapper;
import com.blog.admin.pojo.auth.Permission;
import com.blog.admin.pojo.auth.Role;
import com.blog.admin.pojo.auth.RolePermission;
import com.blog.admin.service.RoleService;
import com.blog.admin.utils.UserUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.hutool.core.util.StrUtil;

@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private PermissionMapper permissionMapper;
	@Autowired
	private RolePermissionMapper rolePermissionMapper;

	@Override
	public Set<String> findRoleNameByUserId(int userId) {
		return roleMapper.findRoleNameByUserId(userId);
	}

	@Override
	public Page<Role> getRoleList(Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return roleMapper.getRoleList();
	}

	@Override
	public List<Map<String, Object>> getMenuTree(String id) {
		List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();

		List<Permission> menus = new LinkedList<Permission>();
		// 管理员显示所有菜单信息 一级菜单
		if (UserUtils.isAdmin()) {
			menus = permissionMapper.getAllPermission();
		} else {
			menus = permissionMapper.getMenuTreesByUserId(UserUtils.currentLoginUser().getId());
		}

		if (StrUtil.isNotBlank(id)) {
			List<String> roleMenuList = permissionMapper.getMenuByrRole(Integer.valueOf(id)); // 根据角色id获取权限
			trees = getTrees(menus, true, roleMenuList, true);
		} else {
			trees = getTrees(menus, false, null, true);
		}
		return trees;
	}

	/**
	 * 对象转菜单树
	 * 
	 * @param menus
	 *            菜单列表
	 * @param isCheck
	 *            是否需要选中
	 * @param roleMenuList
	 *            角色已存在菜单列表
	 * @param permsFlag
	 *            是否需要显示权限标识
	 * @return
	 */
	private List<Map<String, Object>> getTrees(List<Permission> menus, boolean isCheck, List<String> roleMenuList, boolean permsFlag) {
		List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
		for (Permission menu : menus) {
			Map<String, Object> deptMap = new HashMap<String, Object>();
			deptMap.put("id", menu.getId());
			deptMap.put("pId", menu.getParentId());
			deptMap.put("name", transMenuName(menu, roleMenuList, permsFlag));
			deptMap.put("title", menu.getName());
			if (isCheck) {
				deptMap.put("checked", roleMenuList.contains(menu.getId() + menu.getPermission()));
			} else {
				deptMap.put("checked", false);
			}
			trees.add(deptMap);
		}
		return trees;
	}

	private String transMenuName(Permission menu, List<String> roleMenuList, boolean permsFlag) {
		StringBuffer sb = new StringBuffer();
		sb.append(menu.getName());
		if (permsFlag) {
			sb.append("  -->" + menu.getPermission());
		}
		return sb.toString();
	}

	@Override
	public boolean deleteRole(String id) {
		Integer a = roleMapper.deleteRole(Integer.valueOf(id));
		if (a == 1) {
			return true;
		}
		return false;
	}

	@Override
	public Role getRoleById(Integer id) {
		return roleMapper.getRoleById(id);
	}

	@Override
	public void update(Role role, String treeids) {
		roleMapper.update(role);
		// 删除原来的权限设置
		rolePermissionMapper.deleteByRole(role.getId());

		if (StrUtil.isNotBlank(treeids)) {
			String[] ids = treeids.split("\\,");
			List<RolePermission> rplist = new LinkedList<>();
			for (String id : ids) {
				RolePermission rolePermission = new RolePermission();
				rolePermission.setRoleId(role.getId());
				rolePermission.setPermissionId(Integer.valueOf(id));
				rplist.add(rolePermission);
			}
			// 保存权限设置
			rolePermissionMapper.saveRolePermission(rplist);
		}
	}

	@Override
	public void insert(Role role, String treeids) {
		roleMapper.save(role);
		if (StrUtil.isNotBlank(treeids)) {
			String[] ids = treeids.split("\\,");
			List<RolePermission> rplist = new LinkedList<>();
			for (String id : ids) {
				RolePermission rolePermission = new RolePermission();
				rolePermission.setRoleId(role.getId());
				rolePermission.setPermissionId(Integer.valueOf(id));
				rplist.add(rolePermission);
			}
			// 保存权限设置
			rolePermissionMapper.saveRolePermission(rplist);
		}
	}
}
